"use strict";
Object.defineProperty(exports, "__esModule", {value: true});

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

let schemaLog = new Schema({
    type: {
        type: 'String',
        required: true //type: "welcome", "bye"
    },
    userID: {
        type: 'String',
        required: true //userID 非空
    },
    userName: {
        type: 'String',
        required: true //userName 非空
    },
    timeStamp: {
        type: Date,
        default: Date.now
    }
});
exports.schemaLog = schemaLog;

let LogModel = mongoose.model('Log', schemaLog);
exports.LogModel = LogModel;

//插入一条log
exports.insertLog = function (data) {
    console.log("insertLog");

    this.m = data;
    console.log(data);
    //防止错误时破坏数据
    if (this.m.userID == null || typeof (this.m.userID) == 'undefined') {
        console.log("缺少 userID ");
        return;
    }
    if (this.m.userName == null || typeof (this.m.userName) == 'undefined') {
        console.log("缺少 userName ");
        return;
    }

    //var LogModel = mongoose.model('Log', schemaLog);
    var m = new LogModel;

    if (this.m.type == null || typeof (this.m.type) == 'undefined')
        m.type = 'unknown';
    else
        m.type = this.m.type;

    m.userID = this.m.userID;
    m.userName = this.m.userName;

    m.save(function (err) {
        if (err != null) {
            console.log("err888");
            console.log(err);
        }
    });

};
